package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Api(tags = "文件上传")
@RequiredArgsConstructor
public class CommonController {
    // Bean 有三种注入方式，1属性注入 2构造器注入 3setter注入
    private final AliOssUtil aliOssUtil;

    /**
     * 图片上传
     * @return
     */
    @PostMapping("/admin/common/upload")
    public Result upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String postfix = filename.substring(filename.lastIndexOf("."));
        String newFilename = UUID.randomUUID().toString()+ postfix;

        String url = aliOssUtil.upload(file.getBytes(),newFilename);
        return  Result.success(url);

    }

}
